package com.ruoyi.system.service.impl.reseller;

import java.math.BigDecimal;
import java.time.Year;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.TimeStampUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.reseller.VsDramasResellerBind;
import com.ruoyi.system.domain.reseller.VsDramasResellerOrder;
import com.ruoyi.system.mapper.reseller.VsDramasResellerBindMapper;
import com.ruoyi.system.mapper.reseller.VsDramasResellerOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.reseller.VsDramasResellerMapper;
import com.ruoyi.system.domain.reseller.VsDramasReseller;
import com.ruoyi.system.service.reseller.IVsDramasResellerService;

/**
 * 分销商Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-26
 */
@Service
public class VsDramasResellerServiceImpl implements IVsDramasResellerService 
{
    @Autowired
    private VsDramasResellerMapper vsDramasResellerMapper;

    @Autowired
    private VsDramasResellerOrderMapper vsDramasResellerOrderMapper;

    @Autowired
    private VsDramasResellerBindMapper vsDramasResellerBindMapper;

    /**
     * 查询分销商
     * 
     * @param id 分销商主键
     * @return 分销商
     */
    @Override
    public VsDramasReseller selectVsDramasResellerById(Integer id)
    {
        return vsDramasResellerMapper.selectVsDramasResellerById(id);
    }

    /**
     * 查询分销商列表
     * 
     * @param vsDramasReseller 分销商
     * @return 分销商
     */
    @Override
    public List<VsDramasReseller> selectVsDramasResellerList(VsDramasReseller vsDramasReseller)
    {
        return vsDramasResellerMapper.selectVsDramasResellerList(vsDramasReseller);
    }

    /**
     * 新增分销商
     * 
     * @param vsDramasReseller 分销商
     * @return 结果
     */
    @Override
    public int insertVsDramasReseller(VsDramasReseller vsDramasReseller)
    {
        vsDramasReseller.setCreatetime(System.currentTimeMillis());
        vsDramasReseller.setUpdatetime(System.currentTimeMillis());
        return vsDramasResellerMapper.insertVsDramasReseller(vsDramasReseller);
    }

    /**
     * 修改分销商
     * 
     * @param vsDramasReseller 分销商
     * @return 结果
     */
    @Override
    public int updateVsDramasReseller(VsDramasReseller vsDramasReseller)
    {
        vsDramasReseller.setUpdatetime(System.currentTimeMillis());
        return vsDramasResellerMapper.updateVsDramasReseller(vsDramasReseller);
    }

    /**
     * 批量删除分销商
     * 
     * @param ids 需要删除的分销商主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasResellerByIds(Integer[] ids)
    {
        return vsDramasResellerMapper.deleteVsDramasResellerByIds(ids);
    }

    /**
     * 删除分销商信息
     * 
     * @param id 分销商主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasResellerById(Integer id)
    {
        return vsDramasResellerMapper.deleteVsDramasResellerById(id);
    }

    @Override
    public int submitReseller(VsDramasReseller vsDramasReseller) {
        long l = System.currentTimeMillis();
        VsDramasReseller dramasReseller = vsDramasResellerMapper.selectVsDramasResellerById(vsDramasReseller.getId());
        VsDramasResellerOrder order = new VsDramasResellerOrder();
        order.setResellerId(dramasReseller.getId());
        String s = "R" + l + IdUtils.fastSimpleUUID();
        order.setOrderSn(s);
        order.setUserId(Math.toIntExact(vsDramasReseller.getUserId()));
        order.setLangId(dramasReseller.getLangId());
        order.setTimes(dramasReseller.getExpire());
        order.setStatus(2);
        order.setTotalFee(dramasReseller.getPrice());
        order.setPayFee(BigDecimal.valueOf(0));
        order.setCurrency(dramasReseller.getVsLangInfo().getCurrency());
        order.setRemark("后台管理员手动添加");
        order.setPaytime(l);
        JSONObject jsonObject = new JSONObject();
        // 如果过期时间等于0代表不限时间，否则就当前时间+当前套餐的时间秒数
        if (dramasReseller.getExpire() != null && dramasReseller.getExpire() > 0) {
            jsonObject.put("expired_time", l + dramasReseller.getExpire());
        } else if (dramasReseller.getExpire() != null && dramasReseller.getExpire() == 0) {
            jsonObject.put("expired_time", dramasReseller.getExpire());
        }
        // 将 JSONObject 转换为 JSON 字符串
        String jsonString = jsonObject.toJSONString();
        order.setExt(jsonString);
        order.setCreatetime(l);
        order.setUpdatetime(l);
        vsDramasResellerOrderMapper.insertVsDramasResellerOrder(order);
        VsDramasResellerBind bind = new VsDramasResellerBind();
        bind.setUserId(vsDramasReseller.getUserId());
        bind.setResellerId(Long.valueOf(dramasReseller.getId()));
        bind.setLevel(Long.valueOf(dramasReseller.getLevel()));
        if (dramasReseller.getExpire() != null && dramasReseller.getExpire() > 0) {
            bind.setExpiretime(l + (dramasReseller.getExpire() * 1000));
        } else if (dramasReseller.getExpire() != null && dramasReseller.getExpire() == 0) {
            bind.setExpiretime(dramasReseller.getExpire());
        }
        String toJSONString = JSONObject.toJSONString(dramasReseller);
        bind.setResellerJson(toJSONString);
        bind.setCreatetime(l);
        bind.setUpdatetime(l);
        VsDramasResellerBind isBind = new VsDramasResellerBind();
        isBind.setUserId(vsDramasReseller.getUserId());
        List<VsDramasResellerBind> resellerBinds = vsDramasResellerBindMapper.selectVsDramasResellerBindList(isBind);
        // 如果当前用户以前充值过分销商，那就覆盖之前的分销商相关权益信息，否则就新增该用户的分销商相关权益信息
        if (resellerBinds.size() > 0) {
            return vsDramasResellerBindMapper.updateUserResellerBind(bind);
        }
        return vsDramasResellerBindMapper.insertVsDramasResellerBind(bind);
    }
}
